package com.dehui.traders.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dehui.traders.R;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class SharePopView extends PopupWindow{

    private final View contentView;
    private Activity context;
    private TextView tvWechatFriend;
    private TextView tvWechatMoment;

    public SharePopView(Activity context){
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate(R.layout.popup_share_info, null);
        this.setContentView(contentView);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams params = context.getWindow().getAttributes();
                params.alpha = 1f;
                context.getWindow().setAttributes(params);
            }
        });
        initViews();
    }

    private void initViews() {
        tvWechatFriend = contentView.findViewById(R.id.tv_wechat_friend);
        tvWechatMoment = contentView.findViewById(R.id.tv_wechat_moment);
        TextView tvCancel = contentView.findViewById(R.id.tv_cancel);

        tvWechatFriend.setOnClickListener(v -> {

            Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
            if (wechat.isClientValid()) {
                Wechat.ShareParams sp = new Wechat.ShareParams();
                sp.setTitle("V智能");//标题
                sp.setText("股票大数据智能分析,买卖点实时推送");//内容
                sp.setUrl("https://www.pgyer.com/Gvyr");//链接
                Bitmap imageData = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_app);
                sp.setImageData(imageData);
                sp.setShareType(Platform.SHARE_WEBPAGE);
                wechat.share(sp);

            } else {
                ToastUtils.showShort("没有安装微信");
            }
        });

        tvWechatMoment.setOnClickListener(v -> {

            Platform wechat = ShareSDK.getPlatform(WechatMoments.NAME);
            if (wechat.isClientValid()) {
                Wechat.ShareParams sp = new Wechat.ShareParams();
                sp.setTitle("V智能");//标题
                sp.setText("股票大数据智能分析,买卖点实时推送");//内容
                sp.setUrl("https://www.pgyer.com/Gvyr");//链接
                Bitmap imageData = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_app);
                sp.setImageData(imageData);
                sp.setShareType(Platform.SHARE_WEBPAGE);
                wechat.share(sp);

            } else {
                ToastUtils.showShort("没有安装微信");
            }

        });

        tvCancel.setOnClickListener(v -> {
            this.dismiss();
        });
    }

    public void show(){
        this.showAtLocation(context.getWindow().getDecorView(), Gravity.BOTTOM,0,0);
        WindowManager.LayoutParams params = context.getWindow().getAttributes();
        params.alpha = 0.5f;
        context.getWindow().setAttributes(params);
    }

}
